/*
 * Copyright 2014 Rivaldo.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jety.commons.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.StringReader;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import org.jety.commons.domain.ProxyBuilder;
import org.jety.server.AsynchronousHandler;
import org.jety.server.AsynchronousTransport;

/**
 *
 * @author Rivaldo
 */
public class JsonConverter {

    public static JsonObject toJsonObject(String data) {
        JsonParser parser = new JsonParser();
        return (JsonObject) parser.parse(new StringReader(data));
    }

    public static <T> T fromJsonObject(JsonElement data, Type classType) {
        Gson gson = createJsonBuilder();
        return gson.fromJson(data, classType);
    }

    public static Object[] convertParameters(Method method, JsonArray params) {

        List<Object> list = new ArrayList<Object>();
        Gson gson = createJsonBuilder();

        Type[] types = method.getGenericParameterTypes();

        for (int i = 0; i < types.length; i++) {
            JsonElement p = params.get(i);
            Object o = gson.fromJson(p.toString(), types[i]);
            list.add(o);
        }

        return list.toArray();
    }

    public static Object[] convertAsynchronousParameters(int requestID, Method method, JsonArray params, AsynchronousTransport transport) {

        List<Object> list = new ArrayList<Object>();
        Gson gson = createJsonBuilder();

        Class<?>[] types = method.getParameterTypes();

        JsonElement element;

        for (int i = 0; i < types.length; i++) {

            element = params.get(i);

            if (AsynchronousUtil.isCallback(types[i])) {
                AsynchronousHandler asyncHandler = new AsynchronousHandler(requestID, transport);
                Object callback = ProxyBuilder.createProxy(asyncHandler, types[i]);
                list.add(callback);
            } else {
                Object o = gson.fromJson(element.toString(), types[i]);
                list.add(o);
            }
        }

        return list.toArray();
    }

    /**
     * Create a default GsonBuilder.
     * @return A GsonBuilder with map serialization
     */
    public static Gson createJsonBuilder() {
        return new GsonBuilder().enableComplexMapKeySerialization().create();
    }
}
